package com.ifdom.lamada;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class LamadaDemo {
    public static void main(String[] args) {
        referenceDemo7();
    }

    // reduce
    public static void referenceDemo7() {
        Stream<String> integerStream = Stream.of("a", "b", "a", "c", "d", "d", "e");
        String total = integerStream.reduce("", (x, y) -> x + y);

        log.info(total);
    }

    // 转化为数组
    public static void referenceDemo6() {
        Stream<String> integerStream = Stream.of("a", "b", "a", "c", "d", "d", "e");
        String[] arrays = integerStream.filter(item -> item.equals("d")).toArray(String[]::new);

        log.info(Arrays.toString(arrays));
    }

    public static void referenceDemo5() {
        Stream<String> integerStream = Stream.of("a", "b", "a", "c", "d", "d", "e");
        List<String> list = integerStream.filter(item -> item.equals("d")).collect(Collectors.toList());
        System.out.println(list);
    }

    // 转化为List
    public static void referenceDemo4() {
        Stream<String> integerStream = Stream.of("a", "b", "a", "c", "d", "d", "e");
        List<String> list = integerStream.map(item -> item.toUpperCase()).collect(Collectors.toList());
        System.out.println(list);
    }

    public static void referenceDemo3() {
        Stream<String> integerStream = Stream.of("a", "b", "a", "c", "d", "d", "e");
        integerStream.skip(2).limit(2).forEach(item -> System.out.println(item.toUpperCase()));
    }

    public static void referenceDemo2() {
        Stream<Integer> integerStream = Stream.of(2, 2, 100, 5);
        List<Integer> list = integerStream.peek((e) -> System.out.println(e + 2)).collect(Collectors.toList());
        System.out.println(list);
    }

    public static void referenceDemo1() {
        Stream<Integer> integerStream = Stream.of(2, 2, 100, 5);
        Integer max = integerStream.min(Integer::compareTo).get();
        System.out.println(max);
    }


    // 方法引用 https://juejin.cn/post/6844904184953700360
    // 原理例子 MethodReferenceDemo.java
    public static void methodReference() {
        Function<String, Integer> s = Integer::parseInt;
        Integer i = s.apply("10");

        Comparator<Integer> comparator = Integer::compare;
        int result = comparator.compare(100, 10);

        IntBinaryOperator intBinaryOperator = Integer::compare;
        int result2 = intBinaryOperator.applyAsInt(10, 100);
        System.out.println(result2);
    }
}
